package com.yunban.project.template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunban.project.template.mapper.TemplateCollectMapper;
import com.yunban.project.template.model.domain.Template;
import com.yunban.project.template.model.domain.TemplateCollect;
import com.yunban.project.template.model.domain.TemplateGroup;
import com.yunban.project.template.service.TemplateCollectService;
import com.yunban.project.template.service.TemplateGroupService;
import com.yunban.project.template.service.TemplateService;
import jakarta.annotation.Resource;
import net.lab1024.sa.base.common.exception.BusinessException;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author: lijiajun
 * @date: 2024-11-29
 * @version: 1.0
 */
@Service
public class TemplateCollectServiceImpl extends ServiceImpl<TemplateCollectMapper, TemplateCollect> implements TemplateCollectService {

    @Resource
    private TemplateGroupService templateGroupService;

    @Override
    public Boolean collect(long templateGroupId) {
        TemplateGroup templateGroup = this.templateGroupService.findById(templateGroupId);
        // 判断一下是否是公开的模板，假如是的话并且不是自己的模板才可以进行收藏
        long userId = SmartRequestUtil.getUserId();
        if (templateGroup.getCreatedBy() == userId) {
            throw new BusinessException("不能收藏自己的模板~");
        }
        if (!templateGroup.isHasPublic()) {
            throw new BusinessException("无权操作");
        }
        // 判断是否已经收藏了这个模板组
        List<Long> collectList = this.getUserCollectList(userId);
        if (collectList.contains(templateGroupId)) {
            throw new BusinessException("已经收藏过了~");
        }
        TemplateCollect templateCollect = new TemplateCollect();
        templateCollect.setTemplateGroupId(templateGroupId);
        templateCollect.setTemplateName(templateGroup.getName());
        return this.save(templateCollect);
    }

    @Override
    public Boolean removeCollect(long groupId) {
        // 获取用户userId 对应的收藏记录
        LambdaQueryWrapper<TemplateCollect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TemplateCollect::getCreatedBy, SmartRequestUtil.getUserId());
        queryWrapper.eq(TemplateCollect::getTemplateGroupId, groupId);
        TemplateCollect collectRecord = this.getOne(queryWrapper, false);
        if(ObjectUtils.isEmpty(collectRecord)){
            throw new BusinessException("该收藏记录不存在");
        }
        return this.removeById(collectRecord.getId());
    }

    @Override
    public List<Long> getUserCollectList(long userId) {
        LambdaQueryWrapper<TemplateCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TemplateCollect::getCreatedBy, userId);
        return this.list(wrapper).stream().map(TemplateCollect::getTemplateGroupId).toList();
    }
}
